# -*- coding: utf-8 -*-
import os
import pandas as pd
import re
codeql_path='/Users/yy/Documents/2023/OpenSource/codeql/java/ql/src/Security/CWE'
cweid_list=os.listdir(codeql_path)
result={
    "cwe_name":[],
    "cweid":[],
}

for cweid in cweid_list:
    path=codeql_path+'/'+cweid
    file_name_list_in_cwe=os.listdir(path)
    for file_name in file_name_list_in_cwe:
        if file_name[-3:]=='.ql':
            file=open(path+'/'+file_name,'r')
            content=file.read()
            cwe_name=re.search(r'@name (.*)\n',content,flags=0).groups()[0]
            # 把CWE-020的格式改成CWE-20
            cweid='CWE-'+str(int(re.findall(r'CWE-(\d+)',cweid)[0]))
            result['cweid'].append(cweid)
            result['cwe_name'].append(cwe_name)
df = pd.DataFrame(result)
df.to_excel('./resource/codeql_cwe_name.xlsx', sheet_name='cwe')